package com.beadhouse.nurcontent.web.controller;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beadhouse.common.dto.JSONResponse;
import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.NurContentEntity;
import com.beadhouse.nurcontent.ao.NurContentAo;
import com.beadhouse.nurcontent.ao.NurContentModifyAo;
import com.beadhouse.nurcontent.dto.NurContentModifyDto;
import com.beadhouse.nurcontent.service.NurContentService;

@Controller
@Validated
public class NurContentController {
	@Autowired
	NurContentService nurContentService;
	
	@RequestMapping("/hlgl/hlnr/list.do")
	public ModelAndView nurContentList(String key,String pageno) throws Exception {
		ModelAndView mav=new ModelAndView("nurcontent/hlnr");
		int pageNo=1;
		if(pageno!=null) {
			pageNo=Integer.parseInt(pageno);
		}
		QueryResult<NurContentEntity> qr=nurContentService.selectNurContent(key,pageNo);
		List<NurContentEntity> list=qr.getRows();
		Collections.reverse(list);
		mav.addObject("nct",qr);
		return mav;
	}
	@RequestMapping("/hlgl/hlnr/add.do")
	public String addNurContent(@Validated NurContentAo nca,Errors es) throws Exception {
		if(es.hasErrors()) {
			throw new BeadHouseException(es.getAllErrors().get(0).getDefaultMessage());
		}
		nurContentService.addNurContent(nca,1);
		return "forward:/hlgl/hlnr/list.do"; 
	}
	@RequestMapping("/hlgl/hlnr/remove.do")
	public String deleteNurContent(HttpServletRequest req,String id) throws Exception {
		Map<String,Object> map=new HashMap<>();
		System.out.println(id);
		int nid;
		if(id==null||id.length()==0) {
			throw new BeadHouseException("id不能为空");
		}
		nid=Integer.parseInt(id);
		System.out.println(nid);
		map.put("delFlag",true);
		map.put("updateDate",new Date());
		map.put("updateBy", 1);
		nurContentService.deleteNurContent(nid,1, map);
		return "forward:/hlgl/hlnr/list.do"; 
	}
	@RequestMapping("/hlgl/hlnr/modify.do")
	public String modifyNurContent(@Validated NurContentModifyAo ncao,Errors es) throws Exception {
		if(es.hasErrors()) {
			throw new BeadHouseException(es.getAllErrors().get(0).getDefaultMessage());
		}
		Map<String,Object> map=new HashMap<>();
		int id=Integer.parseInt(ncao.getId());
		map.put("name",ncao.getName());
		map.put("price", ncao.getPrice());
		map.put("remarks",ncao.getRemarks());
		map.put("incrementFlag","1".equals(ncao.getIncrement()));
		map.put("status","1".equals(ncao.getStatus()));
		map.put("describe",ncao.getDescribe());
		map.put("updateDate",new Date());
		map.put("updateBy", 1);
		nurContentService.updateNurContent(id, map);
		return "forward:/hlgl/hlnr/list.do";
	}
	@RequestMapping("/api/hlgl/hlnr/onenurcontent")
	@ResponseBody
	public JSONResponse checkOneNurContent(@NotBlank String nurId) throws Exception{
		int id=Integer.parseInt(nurId);
		NurContentModifyDto ncmd=nurContentService.selectOne(id);
		return JSONResponse.ok(ncmd);
	}
}
